package com.xms.security.service.cascade;

import com.xms.core.module.ModuleCollection;
import com.xms.dependency.XmsDependencyException;
import com.xms.schema.event.EntityMetaDeletedEvent;
import com.xms.security.service.dataauth.IRoleObjectAccessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.Collections;

/**
 * <p>
 * 实体权限被删除时，级联删除角色授权数据
 * </p>
 *
 * @author migo
 * @since 2021/10/26
 */
@Service
public class RoleEntityPermissionCascadeDeleter {
    private IRoleObjectAccessService _roleObjectAccessService;

    @Autowired
    public void RoleEntityPermissionCascadeDelete(IRoleObjectAccessService roleObjectAccessService) {
        _roleObjectAccessService = roleObjectAccessService;
    }

    @Async
    @EventListener()
    public void onApplicationEvent(EntityMetaDeletedEvent event) throws XmsDependencyException, SQLException {
        if (event.source == null)
            return;
        _roleObjectAccessService.deleteByObjectId(ModuleCollection.getIdentity("entitypermission"), Collections.singletonList(event.source.getEntityid()));
    }
}
